package com.r2coding.system.controller;

import java.util.Date;
import java.util.List;

import com.r2coding.common.enums.Module;
import com.r2coding.common.enums.OperationType;
import com.r2coding.common.util.Result;
import com.r2coding.system.annotation.SysLog;
import com.r2coding.system.vo.req.PermissionReqVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.r2coding.system.entity.PermissionEntity;
import com.r2coding.system.service.PermissionService;

/**
 * 权限信息控制器
 *
 * @author 98k灬
 * @email hk666zx@163.com
 * @date 2022-04-04 11:10:29
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public Result list() {
        List<PermissionEntity> vos = permissionService.selectList();
        return Result.ok(vos);
    }

    /**
     * 信息
     */
    //@Cacheable(value = "permission", key = " #root.methodName + '_' + #permissionId")
    @RequestMapping("/info/{permissionId}")
    public Result info(@PathVariable("permissionId") Integer permissionId){
		PermissionEntity permission = permissionService.getById(permissionId);
        return Result.ok(permission);
    }

    /**
     * 保存角色权限信息
     */
    @SysLog(module = Module.SYSTEM, operationType = OperationType.INSERT, description = "保存角色权限")
    @PreAuthorize("hasAuthority('permission:add')")
    @RequestMapping("/saveRolePermission")
    public Result saveRolePermission(@RequestBody PermissionReqVo vo){
		permissionService.saveRolePermission(vo);
        return Result.ok();
    }

    /**
     * 保存
     *
     * @param permission 权限信息
     * @return 统一响应体
     */
    @SysLog(module = Module.SYSTEM, operationType = OperationType.INSERT, description = "保存权限")
    @PreAuthorize("hasAuthority('permission:add')")
    @PostMapping("/save")
    public Result save(@RequestBody PermissionEntity permission) {
        permission.setUpdateTime(new Date());
        permission.setCreateTime(new Date());
        permission.setDeleted(0);
        permissionService.save(permission);
        return Result.ok();
    }

    /**
     * 修改
     */
    @SysLog(module = Module.SYSTEM, operationType = OperationType.UPDATE, description = "修改权限")
    @PreAuthorize("hasAuthority('permission:edit')")
    @PutMapping("/update")
    public Result update(@RequestBody PermissionEntity permission){
		permissionService.updateById(permission);
        return Result.ok();
    }

    /**
     * 删除
     */
    @SysLog(module = Module.SYSTEM, operationType = OperationType.DELETE, description = "修改权限")
    @PreAuthorize("hasAuthority('permission:delete')")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable("id") Integer id){
		permissionService.removeById(id);
        return Result.ok();
    }
}
